package com.quyu.controller;


import com.quyu.dto.LoginFormDTO;
import com.quyu.dto.Result;
import com.quyu.dto.UserDTO;
import com.quyu.entity.User;
import com.quyu.entity.UserInfo;
import com.quyu.service.IUserInfoService;
import com.quyu.service.IUserService;
import com.quyu.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IUserInfoService userInfoService;

    /**
     * 发送手机验证码
     */
    @PostMapping("code")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
        // TODO 发送短信验证码并保存验证码
        return userService.sendCode(phone,session);
    }

    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
        // TODO 实现登录功能

        return userService.login(loginForm,session);
    }
    @PostMapping("/passwordlogin")
    public Result passwordlogin(String phone,String password){
        // TODO 实现登录功能

        return userService.passwordlogin(phone,password);
    }

    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public Result logout(){
        // TODO 实现登出功能
        UserHolder.removeUser();
        /*return Result.fail("功能未完成");*/
        return Result.ok("退出登录");
    }

    @GetMapping("/me")
    public Result me(){
        // TODO 获取当前登录的用户并返回
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId){
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }
    @PutMapping("/updatepassword")
    public Result updatepassword(@RequestBody Map<String,String> map){
        // TODO 修改密码
        Long userId = UserHolder.getUser().getId();
        User user = userService.getById(userId);
        String oldPwd = user.getPassword();

        String newPwd = map.get("newPwd");
        String rePwd = map.get("rePwd");
        if(!StringUtils.hasLength(oldPwd)||!StringUtils.hasLength(newPwd)||!StringUtils.hasLength(rePwd)){
            return Result.fail("缺少必要参数");
        }

        if(!newPwd.equals(rePwd)){
            return Result.fail("两次密码不一致");
        }
        if(newPwd.equals(oldPwd)){
            return Result.fail("新密码不能与旧密码相同");
        }
        userService.updatePwd(userId,newPwd);
        return Result.ok();
    }
}

